﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Model.Common;
using Chinaer.Core.Helper;

namespace Chinaer.Bll
{
    public class City : Repository, IBll.ICity
    {
        public City() { }
        Chinaer.IDao.ICityDAO cityDAO = new Chinaer.Dao.CityDAO();
        public List<DTO.CityDTO> GetCityByProvinceID(string provinceID)
        {

            List<Model.Entities.Model_City> listAddress = cityDAO.GetCityByProvinceID(provinceID);
            List<DTO.CityDTO> listCityDTO = new List<DTO.CityDTO>();
            IBll.IProvince province = new Bll.Province();
            foreach (var item in listAddress)
            {
                DTO.CityDTO cityDTO = new DTO.CityDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, cityDTO);
                if (cityDTO != null)
                {
                    DTO.ProvinceDTO provinceDTO = province.GetByID(cityDTO.ProvinceID);
                    if (provinceDTO != null)
                    {
                        cityDTO.ProvinceName = provinceDTO.Name;
                    }
                }
                listCityDTO.Add(cityDTO);
            }
            return listCityDTO;
        }

        public JsonResult Add(DTO.CityDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }

            //判断城市名称是否存在
            if (cityDAO.GetByName(t.CityName) != null)
            {
                return new JsonResult()
                {
                    ErrorMessage = "城市名称已经存在",
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists,
                    Success = false
                };
            }
            if (cityDAO.GetByCode(t.CityCode) != null)
            {
                return new JsonResult()
                {
                    ErrorMessage = "城市编码已经存在",
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists,
                    Success = false
                };
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_City model_City = new Model.Entities.Model_City();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_City);
            if (string.IsNullOrEmpty(model_City.ID) || model_City.ID.Trim() == "")
            {
                model_City.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            jsonResult.Success = cityDAO.Add(model_City);
            return jsonResult;
        }

        public JsonResult Delete(DTO.CityDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_City model_City = new Model.Entities.Model_City();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_City);
            jsonResult.Success = cityDAO.Delete(model_City);
            return jsonResult;
        }

        public JsonResult Delete(string id)
        {
            JsonResult jsonResult = new JsonResult();
            jsonResult.Success = cityDAO.Delete(id);
            return jsonResult;
        }

        public JsonResult Update(DTO.CityDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_City model_City = new Model.Entities.Model_City();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_City);
            jsonResult.Success = cityDAO.Update(model_City);
            return jsonResult;
        }


        public DTO.CityDTO GetByID(string id)
        {
            Model.Entities.Model_City model_City = cityDAO.GetByID(id);
            DTO.CityDTO cityDTO = new DTO.CityDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_City, cityDTO);
            return cityDTO;
        }
    }
}
